#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
商户费率查询模型
"""

from typing import Dict, Any, Optional, List
from dataclasses import dataclass, asdict
from jlpay.model.base import OrgBaseReq, BaseResponse


class RateInfo:
    """费率信息"""
    
    def __init__(self, rate_info_dict=None):
        """
        初始化费率信息
        
        Args:
            rate_info_dict: 费率信息字典
        """
        self.fee_calc_type = None  # 费率计算类型
        self.min_amt = None  # 最小金额
        self.max_amt = None  # 最大金额
        self.fixed = None  # 固定值
        self.rate = None  # 费率
        self.bottom = None  # 底限
        self.top = None  # 上限
        self.step = None  # 步长
        self.base_amount = None  # 基础金额
        self.base_fee = None  # 基础费用
        
        if rate_info_dict:
            self.fee_calc_type = rate_info_dict.get('fee_calc_type')
            self.min_amt = rate_info_dict.get('min_amt')
            self.max_amt = rate_info_dict.get('max_amt')
            self.fixed = rate_info_dict.get('fixed')
            self.rate = rate_info_dict.get('rate')
            self.bottom = rate_info_dict.get('bottom')
            self.top = rate_info_dict.get('top')
            self.step = rate_info_dict.get('step')
            self.base_amount = rate_info_dict.get('base_amount')
            self.base_fee = rate_info_dict.get('base_fee')


class MerchFeeQueryRequest(OrgBaseReq):
    """商户费率查询请求参数"""
    
    API_URL = "/open/merch/access/merch-rate-info/query"
    
    def __init__(self, merch_no: str = ""):
        """初始化商户费率查询请求"""
        super().__init__()
        self.merch_no = merch_no
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        return {k: v for k, v in self.__dict__.items() if not k.startswith('_') and v is not None}


class MerchFeeQueryResponse(BaseResponse):
    """商户费率查询响应参数"""
    
    def __init__(self, response_dict=None):
        """
        初始化响应对象
        
        Args:
            response_dict: 响应字典
        """
        # 先调用父类初始化，处理ret_code, ret_msg等基础字段
        super().__init__(response_dict)
        
        # 初始化特定字段
        self.merch_no = None  # 商户号
        self.rate_info = []  # 费率信息列表
        
        if response_dict:
            # 从response_dict中提取特定字段
            self.merch_no = response_dict.get('merch_no')
            
            # 处理rate_info数组
            rate_info_data = response_dict.get('rate_info', [])
            self.rate_info = []
            for rate_info_dict in rate_info_data:
                rate_info = RateInfo(rate_info_dict)
                self.rate_info.append(rate_info)

    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data['merch_no'] = self.merch_no
        data['rate_info'] = [rate_info.__dict__ for rate_info in self.rate_info]
        return data 